package android.slc.appbase.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.slc.amaputils.AMapLocationByService;
import android.slc.appbase.R;
import android.slc.appbase.config.ConstantsBase;
import android.slc.appbase.vm.GlobalDataVm;
import android.slc.commonlibrary.util.compat.SlcNotifyUtils;
import android.slc.commonlibrary.util.compat.SlcServiceUtils;
import android.slc.rx.SimpleObserver;
import android.slc.rx.SlcRxJavaUtils;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.amap.api.location.AMapLocation;
import com.blankj.utilcode.util.StringUtils;

import org.jetbrains.annotations.NotNull;

/**
 * @author slc
 * @date 2020-09-11 11:00
 */
public class BaseInfoService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * 单次定位
         */
        AMapLocationByService.startSignIn()
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers())
                .subscribe(new SimpleObserver<AMapLocation>() {
                    @Override
                    public void onNext(@NotNull AMapLocation aMapLocation) {
                        super.onNext(aMapLocation);
                        GlobalDataVm.getInstance().locationVmBox.aMapLocationOf.set(aMapLocation);
                    }
                });

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SlcServiceUtils.startForeground(this, ConstantsBase.Value.VALUE_SERVICE_NOTIFY_ID_BASE,
                new NotificationCompat.Builder(this, SlcNotifyUtils.DEF_CHANNEL_ID)
                        .setContentTitle(StringUtils.getString(R.string.app_name))
                        .setContentText(StringUtils.getString(R.string.label_running))
                        .setSmallIcon(R.mipmap.ic_launcher).build());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        super.onDestroy();
    }
}
